package com.climb.spingcloud.controller;


import com.climb.spingcloud.base.R;
import com.climb.spingcloud.dto.ProductDto;
import com.climb.spingcloud.entity.Order;
import com.climb.spingcloud.feign.RemoteProductService;
import com.climb.spingcloud.service.OrderService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author filteraid
 * @since 2021-07-29
 */
@Slf4j
@RestController
@RequestMapping("/order")
public class OrderController_bak {
    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private OrderService orderService;
//    nacos
    @Autowired
    private DiscoveryClient discoveryClient;

    //只要直接注入添加@FeignClient注解的接口,就可以直接调用远程服务
    @Autowired
    private RemoteProductService productService;

    @PostMapping("")
    public R order(Long productId) throws JsonProcessingException {
        log.info(">>客户下单，使用{}号调用商品微服务查询商品信息", productId);
        //1. 查看订单
        //通过 restTemplate 调用商品微服务
        // 返回普通类型的写法-->类型无法转换需要强制转换,转换后仍然无法识别
//        R result = restTemplate.getForObject("http://localhost:57021/products/" + productId, R.class);
//        ProductDto data=(ProductDto) result.getData();
        // 返回泛型类的写法，需要用 ParameterizedTypeReference 声明返回类型



/*        R<ProductDto> result = restTemplate.exchange("http://localhost:57021/products/" + productId,  HttpMethod.GET, null,
                new ParameterizedTypeReference<R<ProductDto>>(){}).getBody();
        ProductDto productDto = result.getData();

        */
//获取服务实例
        List<ServiceInstance> instances = discoveryClient.getInstances("shop-product-service-wjv");
        // 因为只有一个ProductService，因此直接get(0)获取
//        ServiceInstance instance = instances.get(0);



/*
        //自定义规则实现随机挑选服务
        int index =new Random().nextInt(instances.size());
        ServiceInstance instance=instances.get(index);
       // 获取ip和端口信息
        String url = instance.getHost() + ":" + instance.getPort();
        log.info(">>从nacos中获取到的微服务地址为:" + url);
        <ProductDto> result=restTemplate.exchange("http://" + url + "/products/" + productId, HttpMethod.GET, null,
                new ParameterizedTypeReference<R<ProductDto>>() {}).getBody();
        ProductDto productDto=result.getData();
        log.info(">>查询{}号商品信息,查询结果:{}", productId, new ObjectMapper().writeValueAsString(productDto));

*/

/*
        //直接使用微服务名字,从nacos中获取服务地址
        String url="shop-product-service-wjv";

        R<ProductDto> result=restTemplate.exchange("http://" + url + "/products/" + productId, HttpMethod.GET, null,
                new ParameterizedTypeReference<R<ProductDto>>() {}).getBody();
                */

        //通过feign调用商品微服务--最终
        R<ProductDto> result= productService.getById(productId);


        ProductDto productDto=result.getData();
        log.info(">>查询{}号商品信息,查询结果:{}", productId, new ObjectMapper().writeValueAsString(productDto));

        // 2. 下单（创建订单）
        Order order = new Order();
        order.setUserId(1L);
        order.setUserName("测试用户");
        order.setProductId(productDto.getId());
        order.setProductName(productDto.getName());
        order.setProductPrice(productDto.getPrice());

        order.setNumber(1);// 默认1件商品
        //3. 保存订单
        orderService.save(order);
        log.info(">>创建订单成功,订单信息为:{}" + new ObjectMapper().writeValueAsString(order));

        return R.success().message("订单创建成功");
    }

}

